<?php
namespace app\controllers;

use Yii;
use yii\web\Controller; // 导入Controller
use app\common\ErrorCode;
use udokmeci\yii2beanstalk\BeanstalkController;//加入队列控制器
use GatewayClient\Gateway;
use app\models\GameUser;

/*
集成各类通用函数的控制器
包括返回数据、队列输入....
*/

class BaseController extends Controller
{
	
	public function Time(){
        //将系统时间设置成亚洲的时间
		date_default_timezone_set('Asia/Shanghai');
        //date()函数是获取当前时间的格式为 2019-5-25 15:13:00 这样格式
		$datetime = date ( 'Y-m-d H:i:s' );
		$time = date ( 'H:i:s' );
        //获取精确时间到毫秒
        $comps = explode(' ',  microtime());
        $msectime = floatval($comps[0]) * 1000000;
        //每天缓存都会刷新、这样就记录当前设定缓存的时间、到每天的00:00:00时 缓存会自动把当前缓存删除
        $secondtime = explode(':', $time);
        $second = 86400 - $secondtime[0]*3600 - $secondtime[1]*60 - $secondtime[2];
        //将时间储存到关联数组中
        $getup = array(
        	'second' => $second,
        	'msectime' => $msectime,
        	'datetime' => $datetime,
            'time' => $time,
        );
        return $getup;
    }

    //判读四六级的考试时间
    public function EnglishTime(){
        date_default_timezone_set('Asia/Shanghai');
        $datetime = date ( 'Y/m/d' );
        $englishTime = '2018/12/15';
        $count = date_diff(date_create($datetime),date_create($englishTime))->format("%a");
        return $count;
    }

    //用户判断是否存在
    public function judgeSession( $session ){

        $userdata = -1;
        if( isset($session) ){
            $key = '_3rd_session:'.$session;
            $redisinfo = Yii::$app->redis->get($key);
            $openid = BaseController::RedisExplode($redisinfo);
            $userdata = GameUser::data_by_openid($openid);
            if( $userdata == -1 ){
                $info = "数据库找不到用户信息，请重新登录或者联系客服";
                $userdata = array(
                    'code' => ErrorCode::$DATABASE_NOT_FOUND,
                    'code_info' => '数据库查找不到信息',
                    'data' => $info,
                );
            }else if( $userdata == 0 ){
                $userdata = "错误openid，请重新登录或者联系客服";
                $userdata = array(
                    'code' => ErrorCode::$DATA_ERROR,
                    'code_info' => '数据错误',
                    'data' => $info,
                );
            }
        }else{
            $info = "session出错,请尝试重新登录";
            $userdata = array(
                'code' => ErrorCode::$DATA_ERROR,
                'code_info' => "数据出错",
                'data' => $info,
            );
        }
        return $userdata;
    }

    /****
    Gatewayworker 用于对客户进行长链接
    使用长连接可以让用户看到更多动态实时更新信息
    ****/

    //绑定userid和client_id
    public function bind($client_id, $uid){
    	Gateway::$registerAddress = '127.0.0.1:1236';
        // client_id与uid绑定
        Gateway::bindUid($client_id, $uid);
    }
    
    //client_id绑定群组
    public function joinGroup($client_id, $gropid){
        Gateway::$registerAddress = '127.0.0.1:1236';
        Gateway::joinGroup($client_id, $gropid);
    }
    
    //根据userid单独发送个人信息
    public function sendMessage($uid, $message){
    	// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值(ip不能是0.0.0.0)
        Gateway::$registerAddress = '127.0.0.1:1236';
        // 向任意uid的网站页面发送数据
        Gateway::sendToUid($uid, $message);
    }

    //根据client_id单独发送个人信息
    public function sendClientId($clientid, $message){
        Gateway::$registerAddress = '127.0.0.1:1236';
        Gateway::sendToClient($clientid,$message);
    }

    //根据group_id发送群组消息
    public function sendGroupId($groupid, $message){
        Gateway::$registerAddress = '127.0.0.1:1236';
        Gateway::sendToGroup($groupid, $message);
    }

    //回复前端用的代码
    public function SuccessRespone($data){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return [
            'code' => ErrorCode::$SUCCESS,
            'code_info' => '成功',
            'data' => $data,
        ];
    }
    public function FailRespone($data){
        if( $data['0'] == 0 ){//502
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$DATABASE_NOT_FOUND,
                'code_info' => '数据库查找不到信息',
                'data' => $data['1'],
            ];
        }else if( $data['0'] == -1 ){//503
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$DATABASE_ERROR,
                'code_info' => '录入数据库失败',
                'data' => $data['1'],
            ];
        }else if( $data['0'] == -2 ){//504
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$RECEIVE_ERROR,
                'code_info' => '接收数据为空',
            ];
        }else if( $data['0'] == -3){//505
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$DATA_ERROR,
                'code_info' => '数据出错',
            ];
        }
    }
    
    //redis解码
    public function RedisExplode($redisinfo){
    	$logininfo = json_decode($redisinfo);
        $logininfo = get_object_vars($logininfo);
    	$openid = $logininfo['openid'];
    	return $openid;
    }
    
    //判读起床时间是否在活动时间内
    public function JudgeTime($time){
    	$start = Yii::$app->params['WakeUp']['START'];
    	$over = Yii::$app->params['WakeUp']['OVER'];
    	$night = Yii::$app->params['WakeUp']['NIGHT'];
    	$content = "signup";
    	if( $time >= $start && $time <= $over ){//06:30:00-10:30:00
    		$content = 1;
    	}else if( $time < $start ){//00:00:00-06:30:00
    		$content = -1;
    	}else{ //10:30:00-00:00:00
            $content = -2;
        }
        return $content;
    }

    //根据时间来判断所得的积分
    public function JudgeScore($signdata){
    	$start = Yii::$app->params['WakeUp']['START'];
    	$middle_1 = Yii::$app->params['WakeUp']['MIDDLE_1'];
    	$middle_2 = Yii::$app->params['WakeUp']['MIDDLE_2'];
    	$middle_3 = Yii::$app->params['WakeUp']['MIDDLE_3'];
    	$over = Yii::$app->params['WakeUp']['OVER'];
    	$score = 0;
    	$signscore = 0;
        //签到天数+1
        $signdata['signdays'] = $signdata['signdays']+1;
    	if( $signdata['time'] >= $start && $signdata['time'] <= $middle_1 ){
    		$score = 20;
    	}else if( $signdata['time'] >= $middle_1 && $signdata['time'] <= $middle_2 ){
    		$score = 19;
    	}else if( $signdata['time'] >= $middle_2 && $signdata['time'] <= $middle_3 ){
    		$score = 18;
    	}else if( $signdata['time'] >= $middle_3 && $signdata['time'] <= $over ){
    		$score = 17;
    	}else{
    		$score = -1;
    	}
        //判读昨天有没有签到
        if($signdata['yetsign'] == 1){
            //有则连续签到天数+1
        	$signdata['onsign'] = $signdata['onsign']+1;
        }else{
            //没有则连续签到天数恢复为1
        	$signdata['onsign'] = 1;
        }
        
        //判读连续签到天数的数量来进行额外加分
        if( $signdata['onsign'] >= 1 && $signdata['onsign'] <= 5){
        	$signscore = $signdata['onsign']-1;
        }else if( $signdata['onsign'] >= 6 ){
        	$signscore = 5;
        }
        
        //现在得到的总分
        $signdata['totalscore'] = $signdata['totalscore']+$score+$signscore;//总积分
        $score = $score + $signscore;//获得积分
        $signscore = '+'.$signscore;
        $signdata = array(
        	'score' => $score,//获得积分
        	'totalscore' => $signdata['totalscore'],//总积分
            'onsign' => $signdata['onsign'], //连续签到天数
            'signscore' => $signscore,
        	'signdays' => $signdata['signdays'], //已签到天数 
        	);
        return $signdata;
    }

}
